package com.priox.sigar.application.mappers.handler;

import com.priox.sigar.enums.TipoCuenta;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(TipoCuenta.class)
public class TipoCuentaTypeHandler implements TypeHandler<TipoCuenta> {

    public void setParameter(PreparedStatement preparedStatement, int i, TipoCuenta item, JdbcType jdbcType) throws SQLException {
        preparedStatement.setLong(i, item.getCodigo());
    }

    public TipoCuenta getResult(ResultSet resultSet, String s) throws SQLException {
        if(StringUtils.isBlank(resultSet.getString(s))) return null;
        return TipoCuenta.findByCode(new Long(resultSet.getString(s)));
    }

    public TipoCuenta getResult(CallableStatement callableStatement, int i) throws SQLException {
        return TipoCuenta.findByCode(callableStatement.getLong(i));
    }
}
